package com.itheima.reggie.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entify.Category;
import com.itheima.reggie.entify.Dish;
import com.itheima.reggie.entify.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    /**
     * 菜品分页
     */
    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name) {
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>();

        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();

        qw.like(name != null, Dish::getName, name);

        qw.orderByDesc(Dish::getUpdateTime);

        dishService.page(pageInfo, qw);
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");
        List<Dish> records = pageInfo.getRecords();
        List<DishDto> list = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long categoryId = item.getCategoryId();
            //根据id查询对象
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);

    }

    /**
     * 根据id查询菜品
     */
    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        DishDto byIdFlavor = dishService.getByIdFlavor(id);
        return R.success(byIdFlavor);
    }

    /**
     * 修改菜品
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto) {
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }

    /**
     * 删除菜品及批量删除菜品
     */

    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) throws Exception {

        boolean result = dishService.deleteWithFlavor(ids);

        return result?R.success("删除成功"):R.error("菜品正在出售，不能删除");
        



        
    }
    /**
     * 售卖状态
     */
    @PostMapping("/status/{status}")
    public R status(@RequestParam List<Long> ids,Dish dish){
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId,ids);
        dishService.update(dish,qw);
        return R.success("修改状态成功");
    }

    /**
     * 根据条件查询对应的菜品数据
     */
    @GetMapping("/list")
   /* public R list(Dish dish){
        //构造查询条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        qw.eq(Dish::getStatus, 1);
        //构造排序条件
        qw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> list = dishService.list(qw);

        return R.success(list);
        }*/
    //修改接口以致于实现前端页面的菜品展示
    public R getDishesByCategoryId(Long categoryId) {
        LambdaQueryWrapper<Dish> dqw = new LambdaQueryWrapper<>();
        dqw.eq(Dish::getCategoryId, categoryId);
        List<Dish> list = dishService.list(dqw);
        List<DishDto> collect = list.stream().map(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtil.copyProperties(dish, dishDto);

            LambdaQueryWrapper<DishFlavor> fqw = new LambdaQueryWrapper<>();
            fqw.eq(DishFlavor::getDishId, dishDto.getId());
            List<DishFlavor> flavors = dishFlavorService.list(fqw);
            dishDto.setFlavors(flavors);
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(collect);

    }

}

